#include "Transformation.h"
#include "GameObject.h"
#include "Logger.h"
#include "Exception.h"

using namespace game_engine;
using namespace game_engine::components;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

Transformation::Transformation(void)
    : Component(), xPosition(0.0), yPosition(0.0), 
    xRotation(0.0), yRotation(0.0), xScale(1.0), yScale(1.0),
    previousXPos(0.0), previousYPos(0.0)
{
    type = "Transformation";
}

Transformation::Transformation(GameObject *obj, const FLOAT32 xpos, 
                               const FLOAT32 ypos, const FLOAT32 xrot, 
                               const FLOAT32 yrot, const FLOAT32 xscl, 
                               const FLOAT32 yscl)
    : Component(obj), xPosition(xpos), yPosition(ypos), 
    xRotation(xrot), yRotation(yrot), xScale(xscl), yScale(yscl),
    previousXPos(xpos), previousYPos(ypos)
{
    type = "Transformation";
}

Transformation::~Transformation(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void Transformation::revertPosition(void)
{
    xPosition = previousXPos;
    yPosition = previousYPos;
}

///////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////

const Point Transformation::getPosition(void) const
{
    return Point(xPosition, yPosition);
}

const Point Transformation::getPreviousPosition(void) const
{
    return Point(previousXPos, previousYPos);
}

const FLOAT32 Transformation::getXPosition(void) const
{
    return xPosition;
}

const FLOAT32 Transformation::getYPosition(void) const
{
    return yPosition;
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void Transformation::setPosition(const Point &pos)
{
    previousXPos = xPosition;
    previousYPos = yPosition;

    xPosition = pos.getX();
    yPosition = pos.getY();

    if (object->hasComponent("BoxCollider"))
    {
        //moveEvent.notify(object);
        signalPositionUpdated.Fire(object);
        signalNewPosition.Fire(Point(xPosition, yPosition));
    }
}

void Transformation::setXPosition(const FLOAT32 xPos)
{
    previousXPos = xPosition;
    xPosition = xPos;

    if (object->hasComponent("BoxCollider"))
    {
        signalPositionUpdated.Fire(object);
        signalNewPosition.Fire(Point(xPosition, yPosition));
    }
}

void Transformation::setYPosition(const FLOAT32 yPos)
{
    previousYPos = yPosition;
    yPosition = yPos;

    if (object->hasComponent("BoxCollider"))
    {
        signalPositionUpdated.Fire(object);
        signalNewPosition.Fire(Point(xPosition, yPosition));
    }
}

